package yxy.game.pm2.command.camptower;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.camptower.CampTowerHandle;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;

import java.util.ArrayList;

/**
 * 阵营塔-阵容保存接口
 *
 * @ClassName CMD_CampTowerSaveLineup
 * @Author jingtao-zheng
 * @Date 2022/8/1 9:58
 * @Version 1.0
 **/
@MessageType(proto = Command.CampTowerSaveLineup, classtype = CampTowerMessage.CampTowerSaveLineupRequest.class)
public class CMD_CampTowerSaveLineup extends Command<CampTowerMessage.CampTowerSaveLineupRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.CampTowerSaveLineupRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        //获取阵营塔类型
        int type = request.getType().getNumber();
        //接收阵营信息
        CampTowerMessage.Lineup formatrequest = request.getLineup();
        String fu = formatrequest.getFU() == "" ? null : formatrequest.getFU();
        String fm = formatrequest.getFM() == "" ? null : formatrequest.getFM();
        String fd = formatrequest.getFD() == "" ? null : formatrequest.getFD();
        String bu = formatrequest.getBU() == "" ? null : formatrequest.getBU();
        String bm = formatrequest.getBM() == "" ? null : formatrequest.getBM();
        String bd = formatrequest.getBD() == "" ? null : formatrequest.getBD();
        int nb = formatrequest.getNB();

        CampTowerMessage.CampTowerSaveLineupResponse.Builder response = CampTowerMessage.CampTowerSaveLineupResponse.newBuilder();
        Format.FormatType formatType = CampTowerHandle.matchFormatType(type);
        if (formatType == null) {
            send(messageSource, response, Result.ParamError);
            return;
        }
        if (fu == null || fm == null || fd == null || bu == null || bm == null || bd == null) {
            // 必须上阵6名武将
            send(messageSource, response, Result.CampTowerSixGeneralsRequired);
            return;
        }

        //检测阵容是否满足策划要求,由于只是保存阵容,简单的检测,满足第一层的要求就行
        ArrayList<String> campList = new ArrayList<>();
        campList.add(fu);
        campList.add(fm);
        campList.add(fd);
        campList.add(bu);
        campList.add(bm);
        campList.add(bd);
        boolean isPass = CampTowerHandle.checkCamp(campList, type, 1, playerCode);
        if (!isPass) {
            send(messageSource, response, Result.CampTowerDoesNotMeetTheRequirements);
            return;
        }

        FormatHandle formatModule = new FormatHandle();
        //缓存一下老阵容
        Format oldformat = formatModule.get(playerCode, formatType);
        //组成新阵容
        Format format = formatModule.get(playerCode, formatType);
        format.setFu(fu);
        format.setFm(fm);
        format.setFd(fd);
        format.setBu(bu);
        format.setBm(bm);
        format.setBd(bd);
        format.setNb(nb);

        ArrayList<Hero> heros = new ArrayList<>();
        //检测是否有上阵相同武将,检测神器设置是否有误
        Result result = formatModule.check(playerCode, format, heros);
        if (!result.equals(Result.Success)) {
            send(messageSource, response, result);
            return;
        }

        //保存队伍
        Format.set(playerCode, formatType, format);

        response.setType(CampTowerMessage.LineupType.forNumber(type));
        response.setLineup(formatModule.getCampTowerFormat(format));
        HeroHandle heroModule = new HeroHandle();

        //判断阵容是否有改变
        Boolean isChange = getIsChange(oldformat, format, formatModule, playerCode);

        //获取队伍的战力
        long power = new HeroHandle().getCampTowerAllPower(heros, format, playerCode, 1, formatType, isChange);

        for (Hero hero : heros) {
            try {
                HeroMessage.HeroSimple heroSimple = heroModule.getHeroSimple(playerCode, hero, hero.getPower());
                response.addHeros(heroSimple);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        send(messageSource, response, Result.Success);

        //todo 不知道是否需要推送战力或者红点
    }

    private void send(MessageSource messageSource, CampTowerMessage.CampTowerSaveLineupResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

    //判断阵容是否有改变
    private Boolean getIsChange(Format oldformat, Format format, FormatHandle formatModule, String playerCode) {
        ArrayList<String> oldList = formatModule.formatCodesList(playerCode, oldformat);
        ArrayList<String> newList = formatModule.formatCodesList(playerCode, format);

        if (oldList.size() == 0) return true;

        if (oldList.size() != newList.size()) return true;

        for (int i = 0; i < newList.size(); i++) {

            if (!oldList.contains(newList.get(i))) return true;

            if (!newList.get(i).equals(oldList.get(i))) return true;
        }
        return false;
    }
}
